const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId,
      courseScheduleId: options.courseScheduleId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.listMovement('');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  bindChecked: function(e) {
    var index = e.currentTarget.dataset.index;
    
    this.data.movements[index].selected = !this.data.movements[index].selected
    this.setData({
      movements: this.data.movements
    })
  },

  inputName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  search: function () {
    this.listMovement(this.data.name ? this.data.name : '');
  },

  listMovement: function(name) {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/movements?mobile=' + that.data.mobile + '&name=' + name,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          var movements = [];

          if(res.data.data.movements.length > 0) {
            for (let i in res.data.data.movements) {
              movements.push({
                id: res.data.data.movements[i].id,
                name: res.data.data.movements[i].name,
                summary: res.data.data.movements[i].summary,
                selected: false
              })
            }
          }

          that.setData({
            movements: movements
          });

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  formSubmit: function (e) {
    var that = this;
    var movementIds = e.detail.value.movementIds;

    if (movementIds.length == 0) {
      wx.showToast({
        title: '请选择动作',
        icon: 'none',
        duration: 1000
      });
      return;
    } else {
      that.setData({
        disabled: true
      });

      wx.request({
        url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/schedule-coursewares/' + that.data.courseScheduleId + '/add-courseware?mobile=' + that.data.mobile,
        method: 'POST',
        data: movementIds,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 1000,
              mask: true,
              success: function () {
                wx.navigateBack({
                  delta: 1,
                });
              }
            });
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 3000,
              mask: true
            })
            that.setData({
              disabled: false
            });
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
          that.setData({
            disabled: false
          });
        }
      });
    }
  }
})